package com.lmiot.xyclick.Share;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lkx.liandianqinew.R;
import com.lmiot.xyclick.Share.inteface.OnSearchListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xiaoyi.intentsdklibrary.Bean.ShareBean;
import com.xiaoyi.intentsdklibrary.Bean.ShareBeanSqlUtil;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ShareFragmentDetail extends Fragment implements View.OnClickListener {
    private Activity mActivity;
    TextView mIdBottomTip;
    LinearLayout mIdEmpty;
    NestedScrollView mIdNetscrollview;
    private LinearLayoutManager mLayoutManager;
    private List<ShareBean> mNowDataList = new ArrayList();
    private int mPagerNUm;
    RecyclerView mRecyclerView;
    SmartRefreshLayout mRefreshLayout;
    private ShareAdapter mShareAdapter;
    private ShareTypeEnum mShareTypeEnum;
    private Context myContext;

    public ShareFragmentDetail() {
    }

    public ShareFragmentDetail(Context context, ShareTypeEnum shareTypeEnum) {
        this.myContext = context;
        this.mShareTypeEnum = shareTypeEnum;
    }

    @Override // android.support.v4.app.Fragment
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = activity;
    }

    public Activity getMyActivity() {
        return this.mActivity;
    }

    @Override // android.support.v4.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_share_detail, (ViewGroup) null);
        this.mIdEmpty = (LinearLayout) inflate.findViewById(R.id.id_empty);
        this.mRecyclerView = (RecyclerView) inflate.findViewById(R.id.recyclerView);
        this.mIdBottomTip = (TextView) inflate.findViewById(R.id.id_bottom_tip);
        this.mIdNetscrollview = (NestedScrollView) inflate.findViewById(R.id.id_netscrollview);
        this.mRefreshLayout = (SmartRefreshLayout) inflate.findViewById(R.id.refreshLayout);
        this.mIdEmpty.setOnClickListener(this);
        setRresh();
        this.mRecyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.transparent), -1, 40, 99));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.myContext, 1, false);
        this.mLayoutManager = linearLayoutManager;
        this.mRecyclerView.setLayoutManager(linearLayoutManager);
        this.mRecyclerView.setFocusable(false);
        getData();
        return inflate;
    }

    @Override // android.support.v4.app.Fragment
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override // android.support.v4.app.Fragment
    public void onResume() {
        super.onResume();
    }

    private void setRresh() {
        this.mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            /* class com.lmiot.xyclick.Share.ShareFragmentDetail.AnonymousClass1 */

            @Override // com.scwang.smart.refresh.layout.listener.OnRefreshListener
            public void onRefresh(RefreshLayout refreshLayout) {
                ShareBeanSqlUtil.getInstance().delAllByType(ShareFragmentDetail.this.mShareTypeEnum.getTypeCode());
                ShareFragmentDetail.this.getData();
            }
        });
        this.mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            /* class com.lmiot.xyclick.Share.ShareFragmentDetail.AnonymousClass2 */

            @Override // com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
            public void onLoadMore(RefreshLayout refreshLayout) {
                ShareFragmentDetail.this.mPagerNUm++;
                ShareSDK.getInstance().searchFileByType(ShareFragmentDetail.this.myContext.getPackageName(), ShareFragmentDetail.this.mShareTypeEnum.getTypeCode(), ShareFragmentDetail.this.mPagerNUm, new OnSearchListener() {
                    /* class com.lmiot.xyclick.Share.ShareFragmentDetail.AnonymousClass2.AnonymousClass1 */

                    @Override // com.lmiot.xyclick.Share.inteface.OnSearchListener
                    public void result(boolean z, List<ShareBean> list) {
                        if (ShareFragmentDetail.this.isVisible()) {
                            if (z) {
                                if (list == null) {
                                    list = new ArrayList<>();
                                }
                                if (list.size() == 0) {
                                    ShareFragmentDetail.this.mIdBottomTip.setVisibility(View.VISIBLE);
                                    ShareFragmentDetail.this.mRefreshLayout.setEnableLoadMore(false);
                                    ShareFragmentDetail.this.mRefreshLayout.finishLoadMore(true);
                                    ShareFragmentDetail.this.mShareAdapter.setData(ShareFragmentDetail.this.mNowDataList, "");
                                    return;
                                }
                                ShareFragmentDetail.this.mIdBottomTip.setVisibility(View.GONE);
                                ShareFragmentDetail.this.mRefreshLayout.setEnableLoadMore(true);
                                ShareFragmentDetail.this.mRefreshLayout.finishLoadMore(true);
                                ShareBeanSqlUtil.getInstance().addList(list);
                                ShareFragmentDetail.this.mNowDataList.addAll(list);
                                ShareFragmentDetail.this.mShareAdapter.setData(ShareFragmentDetail.this.mNowDataList, "");
                                return;
                            }
                            ShareFragmentDetail.this.mRefreshLayout.finishLoadMore(false);
                        }
                    }
                });
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void getData() {
        this.mPagerNUm = 1;
        this.mIdBottomTip.setVisibility(View.GONE);
        this.mRefreshLayout.setEnableLoadMore(true);
        List<ShareBean> searchByType = ShareBeanSqlUtil.getInstance().searchByType(this.mShareTypeEnum.getTypeCode());
        this.mNowDataList = searchByType;
        if (searchByType.size() == 0) {
            ShareSDK.getInstance().searchFileByType(this.myContext.getPackageName(), this.mShareTypeEnum.getTypeCode(), this.mPagerNUm, new OnSearchListener() {
                /* class com.lmiot.xyclick.Share.ShareFragmentDetail.AnonymousClass3 */

                @Override // com.lmiot.xyclick.Share.inteface.OnSearchListener
                public void result(boolean z, List<ShareBean> list) {
                    if (ShareFragmentDetail.this.isVisible()) {
                        if (z) {
                            ShareFragmentDetail.this.mRefreshLayout.finishRefresh(true);
                            ShareBeanSqlUtil.getInstance().addList(list);
                            ShareFragmentDetail.this.mNowDataList = ShareBeanSqlUtil.getInstance().searchByType(ShareFragmentDetail.this.mShareTypeEnum.getTypeCode());
                        } else {
                            ShareFragmentDetail.this.mRefreshLayout.finishRefresh(false);
                        }
                        ShareFragmentDetail.this.showListView();
                    }
                }
            });
            return;
        }
        this.mRefreshLayout.finishRefresh(2000);
        showListView();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showListView() {
        if (this.mNowDataList == null) {
            this.mNowDataList = new ArrayList();
        }
        try {
            if (this.mNowDataList.size() == 0) {
                this.mIdEmpty.setVisibility(View.VISIBLE);
            } else {
                this.mIdEmpty.setVisibility(View.GONE);
            }
            sortList(this.mNowDataList);
            ShareAdapter shareAdapter = new ShareAdapter(this.myContext, this.mNowDataList, false);
            this.mShareAdapter = shareAdapter;
            this.mRecyclerView.setAdapter(shareAdapter);
            this.mRecyclerView.setNestedScrollingEnabled(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void sortList(List<ShareBean> list) {
        Collections.sort(list, new Comparator<ShareBean>() {
            /* class com.lmiot.xyclick.Share.ShareFragmentDetail.AnonymousClass4 */

            public int compare(ShareBean shareBean, ShareBean shareBean2) {
                if (shareBean.getDown_sum() > shareBean2.getDown_sum()) {
                    return -1;
                }
                return shareBean.getDown_sum() == shareBean2.getDown_sum() ? 0 : 1;
            }
        });
    }

    public void onClick(View view) {
        if (view.getId() == R.id.id_empty) {
            this.mIdEmpty.setVisibility(View.GONE);
            this.mRefreshLayout.autoRefresh();
            getData();
        }
    }
}
